var selectUsrs =[];
var ids = [];
var names = [];

$(function(){
	if($("#name").val()==''){
		if(parent.$("#name").val()!=null &&parent.$("#name").val()!="") $("#name").val(parent.$("#name").val());
		else if(parent.$("#name").html()!=null &&parent.$("#name").html()!="") $("#name").val(parent.$("#name").html());
	}
	
	/** 保存 */
	$('#btnSave').click(function(){
		$('#name').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务计划主题不能为空'
		});
		$('#planAmount').numberbox({
			required : true,
			min:0,
			precision : 2,
			tipPosition : 'bottom',
			missingMessage : '计划使用资金不能为空',
			invalidMessage : '最小为0.00，只能输入两位小数'
		})
		/*$('#remark').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务计划内容不能为空'
		});*/
		/*$('#expectResult').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '预期成果不能为空'
		});*/
		$('#endTime').validatebox({
			required : true,
			tipPosition : 'bottom',
			missingMessage : '任务计划结束时间不能为空'
		});
		if($('#restAmount').val()!=null && $('#restAmount').val()){
			if(parseFloat($('#restAmount').val())<parseFloat($('#planAmount').val())){
				$.messager.alert('提醒', '计划使用资金不得超过任务剩余预算！','info');
				return;
			}
		}
		var valid = $('#form').form('validate');
		if(valid) {
			$('#form').ajaxSubmit({
				url : 'taskPlan/saveTaskPlan.shtml',
				success : function(data) {
					if (!data.success) {
						$.messager.alert('提醒', '保存失败，请联系管理员','info');
					} else {
						$.messager.alert('提醒', '保存成功','info', function(){
							try{
								var _id = $('#id').val();
								var _operate = (_id == '') ? 'add' : 'edit';
								parent.dealTaskPlan(_operate, data.data);
								// 将比对负责人是否在任务当中。如果不是则进行增加
								var parentIds = parent.$('#taskActorIds').val().split(',');
								var parentNames = parent.$('#taskActorNames').val().split(',');
								var pAId = $('#planActorId').val();
								var pAName = $("#planActorId").find("option:selected").text();
								//console.log(parentIds);
								//console.log(parentNames);
								/*var _isExist = false;
								for (var i=0; i < parentIds.length -1; i ++) {
									if (parentIds[i] == pAId) {
										_isExist = true;
										break;
									}
								}*/
								var _isExist = (parentIds.indexOf(pAId) != -1) ? true : false;
								if(!_isExist) {
									var tempIds = parent.$('#taskActorIds').val();
									var tempNames = parent.$('#taskActorNames').val();
									if ('' != tempIds) {
										tempIds = tempIds + ',' + pAId;
										tempNames = tempNames + ',' + pAName;
									} else {
										tempIds = pAId;
										tempNames = pAName;
									}
									parent.$('#taskActorIds').val(tempIds);
									parent.$('#taskActorNames').val(tempNames);
									parent.makeActorSelect();
								}
							}catch(e){
								////console.log(e);
								parent.reLoadPlans();
							}
							parent.closeEasyUiWindow('win');
							$('#form').form('clear');
						});
					}
				}
			});
		}
	});
	
	/** 取消 */
	$('#btnCancel').click(function(){
		$('#form').form('clear');
		parent.closeEasyUiWindow('win');
	});
	
	// 构建负责人选项
	if(parent.$('#taskActorIds').val().length>1){
		ids = parent.$('#taskActorIds').val().split(',');
		names = parent.$('#taskActorNames').val().split(',');
		for(var i=0;i<ids.length;i++){
			var tmp = {};
			tmp.id = ids[i];
			tmp.text = names[i];
			selectUsrs.push(tmp);
			if (i == 0) {
				$("<option value='"+ids[i]+"' selected='selected'>"+names[i]+"</option>").appendTo("#planActorId");
				//$('#planActorName').val(names[i]);
			} else {
				$("<option value='"+ids[i]+"'>"+names[i]+"</option>").appendTo("#planActorId");
			}
			//$("<option value='"+ids[i]+"'>"+names[i]+"</option>").appendTo("#planActorId");
		}
		
		$('#planActorId').change(function(){
			$('#planActorName').val($(this).find("option:selected").text());
		});
	}
	// 选中默认的负责人
	var _origin_planAcotrId = $('#_planActorId').val();
	////console.log(_origin_planAcotrId);
	if (_origin_planAcotrId !='') {
		var _origin_planAcotrName = $('#planActorName').val();
		//console.log(_origin_planAcotrId);
		//console.log(ids);
		if(ids.indexOf(_origin_planAcotrId) != -1) {
			$('#planActorId').find('option[value="' +$('#_planActorId').val() + '"]').attr('selected', true);
		} else {
			$("<option value='"+_origin_planAcotrId+"' selected='selected'>"+_origin_planAcotrName+"</option>").appendTo("#planActorId");
			$('#planActorId').find('option[text="' + _origin_planAcotrName + '"]').attr('selected', true);
		}
	}
	
});

function planCallback(selectId, selectNames) {
	var _id = selectId.substring(0, selectId.length-1);
	var _name = selectNames.substring(0, selectNames.length-1);
	var _isExist = false;
	for (var i=0; i < ids.length -1; i ++) {
		if (ids[i] == _id) {
			_isExist = true;
			break;
		}
	}
	if (!_isExist) {
		$("<option value='"+_id+"' selected='selected'>"+_name+"</option>").appendTo("#planActorId");
	}
	$('#planActorId').val(_id);
	$('#planActorId').find('option[text="' + _name + '"]').attr('selected', true);
	$('#planActorName').val(_name);
}